﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.ScrollLayout;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Interface;

namespace Spinning.Spinning.Scenes.ProcessScene.Store
{
   public class ScrollLayoutElementSpinningForStore : ScrollLayoutElement
    {
        AnimationSprite _des;
        AnimationSprite _spining, _checkBox;
        Sprite _starSprite,_pinkBackground;
        TextEntity _priceText;

        public enum StateType
        {
            Focus, Disable, Normal
        }

        public StateType State_Type
        {
            get;
            set;
        }

        int _spinningPrice;

        public int SpinningPrice
        {
            get 
            {
                return _spinningPrice;
            }
            set 
            {
                _spinningPrice = value;
                if (_priceText!=null)
                _priceText.Value = "" + _spinningPrice;
            }
        }

        public Constants.SpinningType SpiningType { get; set; }

        public ScrollLayoutElementSpinningForStore()
        {
            Height = 125;
            Origin = new Vector2(.5f);
            SpiningType = Constants.SpinningType.Phi;
            State_Type = StateType.Normal;
            SpinningPrice = 200;
        }

        public override void OnLoad()
        {
            _pinkBackground = new Sprite();
            _pinkBackground.textureName = "item_store/pink_bg";
            _pinkBackground.Origin = new Vector2(.5f);
            _pinkBackground.OnLoad();
            Attach(_pinkBackground);

            _des = new AnimationSprite();
            _des.textureName = "item_store/spin_des";
            _des.Origin = new Vector2(.5f);
            _des.OnLoad();
            _des.SetAnimation(new Point(2, 3));
            Attach(_des);
            _spining = new AnimationSprite();
            _spining.textureName = "item_store/st";
            _spining.Origin = new Vector2(.5f);
            _spining.OnLoad();
            _spining.SetAnimation(new Point(3, 4));
            Attach(_spining);

            _checkBox = new AnimationSprite();
            _checkBox.textureName = "item_store/mark";
            _checkBox.Origin = new Vector2(.5f);
            _checkBox.OnLoad();
            _checkBox.SetAnimation(new Point(1, 2));
            Attach(_checkBox);

            _starSprite = new Sprite();
            _starSprite.textureName = "item_store/star_s";
            _starSprite.Origin = new Vector2(.5f);
            _starSprite.ZIndex = Constants.ui;
            _starSprite.OnLoad();
            Attach(_starSprite);

            

            ResourceManager.ContentRoot = "";
            _priceText = new TextEntity();
            _priceText.fontString = "Century Gothic";
            _priceText.OnLoad();
            _priceText.Origin = new Vector2(0, 0.5f);
            _priceText.Color = Color.White;
            _priceText.Static = false;
            _priceText.ZIndex = Constants.debugText;
            _priceText.Scale = new Vector2(1.2f);
            Attach(_priceText);
            _priceText.Value = "" + _spinningPrice;

            _spining.Position = pos - new Vector2(105, 0);
            _des.Position = pos + new Vector2(70, 20);
            _checkBox.Position = pos + new Vector2(130, -24);
            _starSprite.Position = pos + new Vector2(0, -30);
            _priceText.Position = pos + new Vector2(14, -30);
            _pinkBackground.Position = pos;
        }

        Vector2 pos = new Vector2(0, 125 / 2);

        protected override void UpdateSelf(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int spi = Constants.MappingSpinning(SpiningType);
            _des.SetChangeFrameAnimation(new int[] { spi });
            int i;

            switch (State_Type) 
            {
                case StateType.Disable:
                    i = spi * 2;
                    _spining.SetChangeFrameAnimation(new int[] { i });
                    _des.Color = new Color(125, 125, 125);//灰
                    _priceText.Color = new Color(125, 125, 125);//灰
                    _checkBox.SetChangeFrameAnimation(new int[] { 1 });
                    _checkBox.Color = new Color(125, 125, 125);//灰
                    _pinkBackground.Alpha = 0;
                    break;
                case StateType.Focus:
                    i = spi * 2 + 1;
                    _spining.SetChangeFrameAnimation(new int[] { i });
                    _des.Color = new Color(164, 0, 0);//紅
                    _priceText.Color = new Color(164, 0, 0);//紅
                    _checkBox.SetChangeFrameAnimation(new int[] { 0 });
                    _checkBox.Color = Color.White;
                    _pinkBackground.Alpha = 1;
                    break;
                case StateType.Normal:
                    i = spi * 2 + 1;
                    _spining.SetChangeFrameAnimation(new int[] { i });
                    _des.Color = new Color(54, 56, 43);//咖啡
                    _priceText.Color = new Color(54, 56, 43);//咖啡
                    _checkBox.SetChangeFrameAnimation(new int[] { 1 });
                    _checkBox.Color = new Color(54, 56, 43);//咖啡
                    _pinkBackground.Alpha = 0;
                    break;
            }
        }

        public override bool Contain(Vector2 _position)
        {
            Vector2 position = Vector2.Transform(_position, _checkBox.UnProjection);
            Vector2 topLeft = _checkBox.Position - _checkBox.Origin * new Vector2(_checkBox._frameSize.X, _checkBox._frameSize.Y);
            Vector2 rightDown = topLeft + new Vector2(_checkBox._frameSize.X, _checkBox._frameSize.Y);
            bool h = (position.X > topLeft.X && position.X < rightDown.X && position.Y > topLeft.Y && position.Y < rightDown.Y);
            return h;
        }

        public override void OnTouch(Microsoft.Xna.Framework.Input.Touch.TouchLocation touchLocation)
        {
            if (State_Type == StateType.Normal)
                State_Type= StateType.Focus;
            else if (State_Type == StateType.Focus)
                State_Type = StateType.Normal;
        }

    }
}
